import Vue from "vue";
import VueRouter from "vue-router";
import findLast from "lodash/findLast";
import {
  notification
} from "ant-design-vue";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import {
  check,
  isLogin
} from "../utils/auth";

// import Home from "../views/Home.vue";
// import BasicLayout from "../layouts/BasicLayout";
import Forbidden from "../views/403";
import NotFound from "../views/404";

// 解决点击已选中的路由报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

Vue.use(VueRouter);

const routes = [{
    path: "/user",
    hideInMenu: true,
    redirect: "/user/login",
    component: () =>
      import( /* webpackChunkName: "layout" */ "../layouts/UserLayout"),
    children: [{
        path: "login",
        name: "login",
        component: () =>
          import( /* webpackChunkName: "user" */ "../views/User/login.vue")
      },
      {
        path: "register",
        name: "register",
        component: () =>
          import( /* webpackChunkName: "user" */ "../views/User/Register.vue")
      }
    ]
  },
  {
    path: "/",
    component: () =>
      import( /* webpackChunkName: "layout" */ "../layouts/BasicLayout"),
    redirect: "/Home",
    children: [
      {
        path: "/Home",
        name: "Home",
        component: () =>
          import( /* webpackChunkName: "user" */ "../views/Home.vue"),
        meta: {
          icon: "form",
          title: "主页"
        },
      },
      {
        path: "/Notice",
        name: "Notice",
        component: {
          render: h => h("router-view")
        },
        meta: {
          icon: "form",
          title: "公告管理"
        },
        children: [{
            path: "/Notice/publish",
            name: "publish",
            // hideChildrenInMenu: true,
            meta: {
              title: "发布新公告"
            },
            component: () =>
              import(
                /* webpackChunkName: "user" */
                "../views/notice/PublishNotice.vue"
              )
          },
          {
            path: "/Notice/NoticeList",
            name: "NoticeList",
            // hideChildrenInMenu: true,
            meta: {
              title: "公告列表"
            },
            component: () =>
              import(
                /* webpackChunkName: "user" */
                "../views/notice/Notice.vue"
              )
          }
        ]
      },
      {
        path: "/OrgManage",
        name: "OrgManage",
        component: () =>
          import( /* webpackChunkName: "user" */ "../views/org/Org.vue"),
        meta: {
          icon: "form",
          title: "学校管理"
        },
      },
      {
        path: "/Order",
        name: "Order",
        component: () =>
          import( /* webpackChunkName: "user" */ "../views/order/order.vue"),
        meta: {
          icon: "form",
          title: "订单管理"
        },
      },
      {
        path: "/User",
        name: "User",
        component: {
          render: h => h("router-view")
        },
        meta: {
          icon: "form",
          title: "用户"
        },
        children: [{
            path: "/User/UserManage",
            name: "UserManage",
            component: () =>
              import( /* webpackChunkName: "user" */ "../views/UserManage/UserList"),
            meta: {
              icon: "form",
              title: "用户管理"
            },
          },
          {
            path: "/User/UserRegister",
            name: "UserRegister",
            component: () =>
              import( /* webpackChunkName: "user" */ "../views/UserManage/Register"),
            meta: {
              icon: "form",
              title: "用户认证审批"
            },
          },
        ]
      },
      {
        path: "/About",
        name: "About",
        component: () =>
          import( /* webpackChunkName: "user" */ "../views/About.vue"),
        meta: {
          icon: "dashboard",
          title: "关于我们",
          authority: ["admin"]
        }
      }
    ]
  },
  {
    path: "/403",
    name: "403",
    hideInMenu: true,
    component: Forbidden
  },
  {
    path: "*",
    name: "404",
    hideInMenu: true,
    component: NotFound
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

// 路由跳转钩子
router.beforeResolve((to, from, next) => {
  if (to.path !== from.path) {
    NProgress.start();
  }
  const record = findLast(to.matched, record => record.meta.authority);
  if (record && !check(record.meta.authority)) {
    if (!isLogin() && to.path !== "/user/login") {
      next({
        path: "/user/login"
      });
    } else if (to.path !== "/403") {
      notification.error({
        message: "403",
        description: "您没有权限访问，请联系管理员咨询。"
      });
      next({
        path: "/403"
      });
    }
    NProgress.done();
  }
  next();
});

// 路由跳转钩子
router.afterEach(() => {
  NProgress.done();
});

export default router;